package com.tch.task;

import com.alibaba.fastjson.JSONObject;
import com.tch.entity.Plain;
import com.tch.entity.RefundPay;
import com.tch.mapper.OrderMapper;

import com.tch.util.HttpUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 定时任务
 * Component 注入容器
 */
@Component
@Slf4j
public class ScheduleTasks {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private HttpUtils httpUtils;
    /**
     * 每隔3秒定时执行
     * 3/3 * * * * ?
     */
//    @Scheduled(fixedRate = 9000)
    @SneakyThrows
    @Scheduled(cron = "5/5 * * * * ? ")
    public void taskService() {
        Date day=new Date();
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int count = orderMapper.findcount();
        if(count>0){
            Plain plain = orderMapper.findinfo();
            log.info("<定时执行订单X>>>>>"+sdf.format(day)+">>>"+plain.getOrderNo());
            String url = "http://wsbs.shxga.gov.cn/xabankpay/crj_bank_circle.jsp?order_no="+plain.getOrderNo();
            httpUtils.accessPlatformService(url,plain.toString());
        }
    }

    //每分钟执行
    @Scheduled(cron = "0 * * * * ? ")
    public void taskService2() {
        Map<String, String> map = new HashMap<String, String>();
        JSONObject json = new JSONObject();
        String id = "";
        String orderNo="";
        String payLogId="";
        String payType="";
        String refundAmt="";
        String refundSeq="";
        String refundType="";
        String busNo= "";
        Date day=new Date();
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int count = orderMapper.refundPayCount();
        if (count>0){
            RefundPay pay = orderMapper.crjRefundPay();
            json.put("id",pay.getId());
            json.put("order_no",pay.getOrderNo());
            json.put("refund_seq",pay.getRefundSeq());
            json.put("refund_amt",pay.getRefundAmt());
            json.put("refund_type",pay.getRefundType());
            json.put("pay_type",pay.getPayType());
            json.put("bus_no",pay.getBusNo());
            json.put("pay_log_id",pay.getPayLogId());
            //log.info(json.toString());
            log.info("<定时执行出入境退款>>>>>"+sdf.format(day)+">>>"+pay.getOrderNo()+"busNo:"+pay.getBusNo());
            String url = "http://wsbs.shxga.gov.cn/xabankpay/crjreq_Refund.jsp";
            httpUtils.accessPlatformService(url,json.toString());
        }
    }

    //每15秒执行
    @Scheduled(cron = "15/15 * * * * ? ")
    public void taskService3() {
        //补正告知
        String url = "http://wsbs.shxga.gov.cn/OneThingATime/complete_corr_inform.jsp";
        Date day=new Date();
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<String> list = orderMapper.getSdono();
        String sdono = "";
        if(list.size()>0){
            for(int i=0;i<list.size();i++){
                sdono = list.get(i).toString();
                if(StringUtils.isNotEmpty(sdono)){
                    log.info("<定执一件事办理14>>>>>"+sdf.format(day)+">>>"+sdono);
                    httpUtils.accessPlatformService(url,sdono);
                }
            }
        }else{
            //log.info("<定执一件事办理>>>>>"+sdf.format(day)+">>>无sdono");
        }
    }


    //每10秒执行
    @Scheduled(cron = "10/10 * * * * ? ")
    public void taskService4() {
        //结果反馈
        String url = "http://wsbs.shxga.gov.cn/OneThingATime/accept_result_feedback.jsp";
        Date day=new Date();
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<String> list = orderMapper.getSdono12();
        String sdono = "";
        if(list.size()>0){
            for(int i=0;i<list.size();i++){
                sdono = list.get(i).toString();
                if(StringUtils.isNotEmpty(sdono)){
                    log.info("<定执一件事办理12>>>>>"+sdf.format(day)+">>>"+sdono);
                    httpUtils.accessPlatformService(url,sdono);
                }
            }
        }else{
            //log.info("<定执一件事办理>>>>>"+sdf.format(day)+">>>无sdono");
        }
    }



    /**
     * https://www.bejson.com/othertools/cron/   这个网址自己设置
     */
}
